﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using SqlSugar;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace Nuctech.ACCT.OIS.SqlSugar.AdaptAbpRepository
{
    /// <summary>
    /// 融合了SqlSugar、IRepository、RepositoryAsyncExtensions的常用方法，后面也可能会加一些自定义常用的方法
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public interface ISqlSugarRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity, new()
    {
        #region SqlSugar

        /// <summary>
        /// 对外提供查询操作(支持自动切换数据库)
        /// </summary>
        ISugarQueryable<TEntity> DbQueryable { get; }

        #endregion

        #region RepositoryAsyncExtensions扩展常用方法迁移

        Task<bool> ContainsAsync(TEntity item);

        Task<bool> AnyAsync();

        Task<bool> AnyAsync(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>

        Task<bool> AllAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<int> CountAsync();

        Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression, bool isExtensions = true);

        Task<TEntity> FirstAsync();

        Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<TEntity?> FirstOrDefaultAsync();

        Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<TEntity> SingleAsync();

        Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<TEntity?> SingleOrDefaultAsync();

        Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<TResult> MinAsync<TResult>(Expression<Func<TEntity, TResult>> whereExpression);

        Task<TResult> MaxAsync<TResult>(Expression<Func<TEntity, TResult>> whereExpression);

        Task<decimal> SumAsync(Expression<Func<TEntity, decimal>> whereExpression);

        Task<decimal?> SumAsync(Expression<Func<TEntity, decimal?>> whereExpression);

        Task<int> SumAsync(Expression<Func<TEntity, int>> whereExpression);

        Task<int?> SumAsync(Expression<Func<TEntity, int?>> whereExpression);

        Task<long> SumAsync(Expression<Func<TEntity, long>> whereExpression);

        Task<long?> SumAsync(Expression<Func<TEntity, long?>> whereExpression);

        Task<double> SumAsync(Expression<Func<TEntity, double>> whereExpression);

        Task<double?> SumAsync(Expression<Func<TEntity, double?>> whereExpression);

        Task<float> SumAsync(Expression<Func<TEntity, float>> whereExpression);

        Task<float?> SumAsync(Expression<Func<TEntity, float?>> whereExpression);

        Task<decimal> AverageAsync(Expression<Func<TEntity, decimal>> whereExpression);

        Task<decimal?> AverageAsync(Expression<Func<TEntity, decimal?>> whereExpression);

        Task<double> AverageAsync(Expression<Func<TEntity, int>> whereExpression);

        Task<double?> AverageAsync(Expression<Func<TEntity, int?>> whereExpression);

        Task<double> AverageAsync(Expression<Func<TEntity, long>> whereExpression);

        Task<double?> AverageAsync(Expression<Func<TEntity, long?>> whereExpression);

        Task<double> AverageAsync(Expression<Func<TEntity, double>> whereExpression);

        Task<double?> AverageAsync(Expression<Func<TEntity, double?>> whereExpression);

        Task<float?> AverageAsync(Expression<Func<TEntity, float?>> whereExpression);

        Task<List<TEntity>> ToListAsync();

        Task<TEntity[]> ToArrayAsync();

        #endregion
    }

    /// <summary>
    /// 领域层将IRepository<TEntity, TKey>替换成ISqlSugarRepository<TEntity, TKey>
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public interface ISqlSugarRepository<TEntity, TKey> : ISqlSugarRepository<TEntity>, IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
    {

    }
}
